/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Tag;
import entity.Tagcorrelations;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.NonexistentEntityException;

/**
 *
 * @author atap
 */
public class TagcorrelationsJpaController implements Serializable {

    public TagcorrelationsJpaController(){
        
    }
    
    public TagcorrelationsJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("EducationXPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tagcorrelations tagcorrelations) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tag referencedTagId = tagcorrelations.getReferencedTagId();
            if (referencedTagId != null) {
                referencedTagId = em.getReference(referencedTagId.getClass(), referencedTagId.getId());
                tagcorrelations.setReferencedTagId(referencedTagId);
            }
            Tag mainTagId = tagcorrelations.getMainTagId();
            if (mainTagId != null) {
                mainTagId = em.getReference(mainTagId.getClass(), mainTagId.getId());
                tagcorrelations.setMainTagId(mainTagId);
            }
            em.persist(tagcorrelations);
            if (referencedTagId != null) {
                referencedTagId.getTagcorrelationsList().add(tagcorrelations);
                referencedTagId = em.merge(referencedTagId);
            }
            if (mainTagId != null) {
                mainTagId.getTagcorrelationsList().add(tagcorrelations);
                mainTagId = em.merge(mainTagId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tagcorrelations tagcorrelations) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tagcorrelations persistentTagcorrelations = em.find(Tagcorrelations.class, tagcorrelations.getId());
            Tag referencedTagIdOld = persistentTagcorrelations.getReferencedTagId();
            Tag referencedTagIdNew = tagcorrelations.getReferencedTagId();
            Tag mainTagIdOld = persistentTagcorrelations.getMainTagId();
            Tag mainTagIdNew = tagcorrelations.getMainTagId();
            if (referencedTagIdNew != null) {
                referencedTagIdNew = em.getReference(referencedTagIdNew.getClass(), referencedTagIdNew.getId());
                tagcorrelations.setReferencedTagId(referencedTagIdNew);
            }
            if (mainTagIdNew != null) {
                mainTagIdNew = em.getReference(mainTagIdNew.getClass(), mainTagIdNew.getId());
                tagcorrelations.setMainTagId(mainTagIdNew);
            }
            tagcorrelations = em.merge(tagcorrelations);
            if (referencedTagIdOld != null && !referencedTagIdOld.equals(referencedTagIdNew)) {
                referencedTagIdOld.getTagcorrelationsList().remove(tagcorrelations);
                referencedTagIdOld = em.merge(referencedTagIdOld);
            }
            if (referencedTagIdNew != null && !referencedTagIdNew.equals(referencedTagIdOld)) {
                referencedTagIdNew.getTagcorrelationsList().add(tagcorrelations);
                referencedTagIdNew = em.merge(referencedTagIdNew);
            }
            if (mainTagIdOld != null && !mainTagIdOld.equals(mainTagIdNew)) {
                mainTagIdOld.getTagcorrelationsList().remove(tagcorrelations);
                mainTagIdOld = em.merge(mainTagIdOld);
            }
            if (mainTagIdNew != null && !mainTagIdNew.equals(mainTagIdOld)) {
                mainTagIdNew.getTagcorrelationsList().add(tagcorrelations);
                mainTagIdNew = em.merge(mainTagIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tagcorrelations.getId();
                if (findTagcorrelations(id) == null) {
                    throw new NonexistentEntityException("The tagcorrelations with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tagcorrelations tagcorrelations;
            try {
                tagcorrelations = em.getReference(Tagcorrelations.class, id);
                tagcorrelations.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tagcorrelations with id " + id + " no longer exists.", enfe);
            }
            Tag referencedTagId = tagcorrelations.getReferencedTagId();
            if (referencedTagId != null) {
                referencedTagId.getTagcorrelationsList().remove(tagcorrelations);
                referencedTagId = em.merge(referencedTagId);
            }
            Tag mainTagId = tagcorrelations.getMainTagId();
            if (mainTagId != null) {
                mainTagId.getTagcorrelationsList().remove(tagcorrelations);
                mainTagId = em.merge(mainTagId);
            }
            em.remove(tagcorrelations);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tagcorrelations> findTagcorrelationsEntities() {
        return findTagcorrelationsEntities(true, -1, -1);
    }

    public List<Tagcorrelations> findTagcorrelationsEntities(int maxResults, int firstResult) {
        return findTagcorrelationsEntities(false, maxResults, firstResult);
    }

    private List<Tagcorrelations> findTagcorrelationsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tagcorrelations.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tagcorrelations findTagcorrelations(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tagcorrelations.class, id);
        } finally {
            em.close();
        }
    }

    public int getTagcorrelationsCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tagcorrelations> rt = cq.from(Tagcorrelations.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
